//
//  BQYLGoodsViewController.m
//  BeeQuick
//
//  Created by apple on 16/11/23.
//  Copyright © 2016年 six_group. All rights reserved.
//

#import "BQYLGoodsViewController.h"
#import "BQYLDetailsViewController.h"

#import "BQHomeGoodModel.h"
#import "BQYLDownView.h"




#import <UMSocialCore/UMSocialCore.h>
@interface BQYLGoodsViewController ()<UIPageViewControllerDelegate,UIPageViewControllerDataSource>

@end

@implementation BQYLGoodsViewController
{
    BQHomeGoodModel *_goodModel;
    BQYLDownView *_downView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToBuyCarVc:) name:@"jumpToBuyCar" object:nil];
    
    _goodModel = self.goodsList[_currentIndexPath.section][_currentIndexPath.row];
    
    // 设置导航条
    
    [self setupNav];
    
    // 设置pageViewController
    [self setUpPageController];
}

- (void)pushToBuyCarVc:(NSNotification*)no
{
    [self.tabBarController setSelectedIndex:2];
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed
{
    self.title = _goodModel.name;
    _downView.model = _goodModel;
}


#pragma mark UIPageViewControllerDataSource
//返回前一个控制器
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(BQYLDetailsViewController *)viewController
{
    NSIndexPath *indexPath = viewController.currentIndexPath;
    
    NSInteger row = indexPath.row;
    NSInteger section = indexPath.section;
    row -= 1;
    
    if (row < 0) {
        
        section -= 1;
        
        if (section < 0) {
            section = self.goodsList.count - 1;
        }
        
        row = [self.goodsList[section] count] - 1;
    }
    
    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
    BQHomeGoodModel *goodModel = self.goodsList[section][row];
    
    BQYLDetailsViewController *vc = [[BQYLDetailsViewController alloc]initWithGoodModel:goodModel andIndexPath:newIndexPath];
    
    _goodModel =goodModel;
    
    return vc;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(BQYLDetailsViewController *)viewController
{
    NSIndexPath *indexPath = viewController.currentIndexPath;
    NSInteger row = indexPath.row;
    NSInteger section = indexPath.section;
    row += 1;
    
    if (row >= [self.goodsList[section] count]) {
        
        section += 1;
        
        if (section >= self.goodsList.count) {
            section = 0;
        }
        row = 0;
    }
    
    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
    BQHomeGoodModel *goodModel = self.goodsList[section][row];
    
    BQYLDetailsViewController *vc = [[BQYLDetailsViewController alloc]initWithGoodModel:goodModel andIndexPath:newIndexPath];
    
    _goodModel =goodModel;
    
    
    return vc;
}

#pragma mark --pageController
- (void)setUpPageController
{
    //1.创建vc
   UIPageViewController *pvc = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
        
        //2.设置代理
        pvc.delegate = self;
        pvc.dataSource = self;
    
    //  取出选中的模型
    
        //3.创建内容控制器
    BQYLDetailsViewController *dVc = [[BQYLDetailsViewController alloc]initWithGoodModel:_goodModel andIndexPath:_currentIndexPath];
    
        
        NSArray *viewControllers = @[dVc];
        
        //4.设置内容控制器
        
        [pvc setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
        
        //5.添加视图  设置视图的位置
        
        [self.view addSubview:pvc.view];
        
        [pvc.view mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(self.mas_topLayoutGuide);
            make.left.right.equalTo(self.view);
            make.bottom.equalTo(self.view).offset(-38);
        }];
        
        //6.完成子控制器的添加
        [pvc didMoveToParentViewController:self];
        
        //7.成为子控制器
        [self addChildViewController:pvc];
        
        //8.记录属性
//        _pvc = pvc;
    
    self.title = _goodModel.name;
    
    
    //下边的view
    _downView = [[[NSBundle mainBundle]loadNibNamed:@"BQYLDownView" owner:nil options:nil]lastObject];
    
    _downView.model = _goodModel;
    
    [self.view addSubview:_downView];
    
    [_downView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.left.right.equalTo(self.view);
        
        make.height.mas_equalTo(38);
        
    }];

}


- (void)setupNav
{
    //上按钮
    UIBarButtonItem *backBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"v2_goback"] style:UIBarButtonItemStylePlain target:self action:@selector(jumpMarket)];
    
    self.navigationItem.leftBarButtonItem = backBtn;
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"分享" style:UIBarButtonItemStyleDone target:self action:@selector(shareUI)];
    
}

- (void)jumpMarket
{
    
    [self.navigationController popViewControllerAnimated:YES];
}

// 设置分享界面
- (void)shareUI
{
    UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您要分享吗" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"新浪微博" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self shareWithIndex:0];
        
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"朋友圈" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self shareWithIndex:2];
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"QQ空间" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self shareWithIndex:5];

    }];
    
    UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [vc addAction:action1];
    [vc addAction:action2];
    [vc addAction:action3];
    [vc addAction:action4];
    
    [self presentViewController:vc animated:YES completion:nil];
}


#pragma mark  根据标号   分享到不同的平台
- (void)shareWithIndex:(NSInteger)index
{
    
    //    UMSocialPlatformType_Sina  0
    //    UMSocialPlatformType_WechatSession      = 1, //微信聊天
    //    UMSocialPlatformType_WechatTimeLine     = 2,//微信朋友圈
    //    UMSocialPlatformType_WechatFavorite     = 3,//微信收藏
    //    UMSocialPlatformType_QQ                 = 4,//QQ聊天页面
    //    UMSocialPlatformType_Qzone              = 5,//qq空间
    
    NSString *content = [NSString stringWithFormat:@"%@ --在搞活动,原价%@,现价%@,欢迎购买  我得睡觉了",_goodModel.name,_goodModel.market_price,_goodModel.price];
    
    UMSocialMessageObject *manager = [UMSocialMessageObject messageObject];
     manager.text = content;
    if (index == 0) {
        
        //创建网页内容对象
//        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"分享标题" descr:@"分享内容描述" thumImage:[UIImage imageNamed:@"icon"]];
//        //设置网页地址
//        shareObject.webpageUrl =@"http://mobile.umeng.com/social";
        //创建图片内容对象
        UMShareImageObject *shareImageObject = [[UMShareImageObject alloc] init];
        //如果有缩略图，则设置缩略图
        [shareImageObject setShareImage:_goodModel.img];
        manager.shareObject = shareImageObject;
    }
    

    [[UMSocialManager defaultManager] shareToPlatform:index messageObject:manager currentViewController:self completion:^(id result, NSError *error) {
        if (!error) {
            NSLog( @"分享成功");
        }
    }];
    
}


@end
